package com.wetao.module_report.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.wetao.module_report.R;
import com.wetao.module_report.R2;
import com.wetao.module_report.adapter.ReportMain_h_Adapter;
import com.wetao.module_report.adapter.ReportMain_v_Adapter;
import com.wetao.module_report.contract.ReportSingleFragmentCon;
import com.wetao.module_report.presenter.ReportSingleFragmentPre;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.base.BaseNestingLazyFragment;
import com.wetao.lib_common.bean.response.GetSubjectList;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.contract.EmptyCon;
import com.wetao.lib_common.presenter.EmptyPre;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.MyDividerItemDecoration;
import com.wetao.lib_common.utils.SPUtils;
import com.wetao.lib_common.utils.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class ReportSingleFragment extends BaseNestingLazyFragment<ReportSingleFragmentCon.View, ReportSingleFragmentPre>
        implements ReportSingleFragmentCon.View  {

    private static final String ARG_PARAM1 = "param1";
    private OnFragmentInteractionListener mListener;

    List<GetSubjectList> mSubjectList;
    List<StudentExamListInfo.Data.RecordsBean> mStudentExamList = new ArrayList<>();;
    @BindView(R2.id.srl_refresh)
    SmartRefreshLayout srl_refresh;
    @BindView(R2.id.rv_h)
    RecyclerView rv_h;
    @BindView(R2.id.rv_v)
    RecyclerView rv_v;
    @BindView(R2.id.iv_no_data)
    ImageView iv_no_data;
    @BindView(R2.id.tv_no_data)
    TextView tv_no_data;
    private ReportMain_h_Adapter mReportMain_h_Adapter;
    private ReportMain_v_Adapter mReportMain_v_Adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private MyDividerItemDecoration mDividerItemDecoration;
    private int mCurSubjectPosition = 0;
    private int current = 1;
    private int pages = 1;
    private int size = 10;
    private int total = 1;
    private String studentId;

    public ReportSingleFragment() {
    }

    public static ReportSingleFragment newInstance() {
        ReportSingleFragment fragment = new ReportSingleFragment();
        //Bundle args = new Bundle();
        //args.putBoolean(ARG_PARAM1, param1);
        //fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.report_single_fragment;
    }

    @Override
    public ReportSingleFragmentPre createPresenter() {
        return new ReportSingleFragmentPre(mContext);
    }

    @Override
    public ReportSingleFragmentCon.View createView() {
        return this;
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.e("onAttach");
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.e("onDetach");
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
    }

    @Override
    public void onFragmentVisble() {
        super.onFragmentVisble();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onFragmentInVisible() {
        super.onFragmentInVisible();
    }

    @Override
    public void studentExamListResult(StudentExamListInfo response, int cur) {
        finishSmart(cur);
        if(response.getData() != null) {
            current = cur;
            pages = response.getData().getPages();
            size = response.getData().getSize();
            total = response.getData().getTotal();
            mStudentExamList.addAll(response.getData().getRecords());

            mReportMain_v_Adapter.updateData(mStudentExamList);
            if(mStudentExamList.size() == 0) {
                iv_no_data.setVisibility(View.VISIBLE);
                tv_no_data.setVisibility(View.VISIBLE);
                rv_v.setVisibility(View.GONE);
                tv_no_data.setText("暂无单次学情~");

            } else {
                iv_no_data.setVisibility(View.GONE);
                tv_no_data.setVisibility(View.GONE);
                rv_v.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void studentExamListFailed(int cur) {
        finishSmart(cur);
    }

    @Override
    public void init(View rootView) {
        /*if (getArguments() != null) {
            isYes = getArguments().getBoolean(ARG_PARAM1);
        }*/

        initRecyclerView();
        initSmartRefresh();
        String str = SPUtils.getInstance().getString(Constants.GET_SUBJECT_LIST);
        studentId = SPUtils.getInstance().getString(Constants.USER_ID_KEY);
        mSubjectList = JsonUtils.fromJsonList(str, new TypeToken<List<GetSubjectList>>() {}.getType());

        if( mSubjectList.size() > 0) {
            mReportMain_h_Adapter.setmCurPosition(0);
            mReportMain_h_Adapter.updateData(mSubjectList);
            getStudentExamList(1);
        }

    }

    public void getStudentExamList(int cur) {
        getPresenter().studentExamList(ComUtil.getHashmapByStr(
                new String[]{"itemSubject", "pageNo", "pageSize"},
                new String[]{mSubjectList.get(mCurSubjectPosition).getSubjectId() +"", cur + "", "10"}), false, false, cur);
    }

    private void initSmartRefresh() {
        srl_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.i("---加载更多");
                if(pages>current){
                    getStudentExamList(current + 1);
                }else {
                    refreshLayout.getLayout().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        }
                    }, 1000);
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.i("---下拉刷新");
                getStudentExamList(1);
            }
        });
        srl_refresh.setRefreshHeader(new ClassicsHeader(getActivity()));
        srl_refresh.setRefreshFooter(new ClassicsFooter(getActivity()));
    }

    public void resetNoMoreData(){
        if(srl_refresh!=null){
            srl_refresh.resetNoMoreData();//去除没有更多数据状态
        }
    }

    public void finishSmart(int cur) {
        if(cur == 1) {
            resetNoMoreData();
            mStudentExamList.clear();
        }
        if(srl_refresh!=null){
            srl_refresh.getLayout().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(srl_refresh != null) {
                        srl_refresh.finishRefresh();
                        srl_refresh.finishLoadMore();
                    }
                }
            }, 500);
        }
    }

    private void initRecyclerView() {
        mLayoutManager = new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mReportMain_h_Adapter = new ReportMain_h_Adapter(mContext);
        rv_h.setLayoutManager(mLayoutManager);
        mDividerItemDecoration = new MyDividerItemDecoration(getActivity(), LinearLayoutManager.HORIZONTAL, R.drawable.divider_mileage_voice_class);
        //rv_h.addItemDecoration(mDividerItemDecoration);
        rv_h.setItemAnimator( new DefaultItemAnimator());
        rv_h.setAdapter(mReportMain_h_Adapter);
        ((ReportMain_h_Adapter)mReportMain_h_Adapter).setOnItemClickListener(new ReportMain_h_Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                LogUtils.i("onItemClick--" + position);
                mCurSubjectPosition = position;
                getStudentExamList(1);
            }
        });


        mLayoutManager = new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mReportMain_v_Adapter = new ReportMain_v_Adapter();
        rv_v.setLayoutManager(mLayoutManager);
        mDividerItemDecoration = new MyDividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL, R.drawable.divider_mileage_voice_class);
        //rv_v.addItemDecoration(mDividerItemDecoration);
        rv_v.setItemAnimator( new DefaultItemAnimator());
        rv_v.setAdapter(mReportMain_v_Adapter);
        ((ReportMain_v_Adapter)mReportMain_v_Adapter).setOnItemClickListener(new ReportMain_v_Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                LogUtils.i("onItemClick--" + position);
                if(position >= mStudentExamList.size()) {
                    return;
                }
                StudentExamListInfo.Data.RecordsBean item = mStudentExamList.get(position);

                ARouter.getInstance().build(ConstantArouter.PATH_REPORT_SINGLE_DETAIL_ACTIVITY)
                        .withInt("subjectId", mSubjectList.get(mCurSubjectPosition).getSubjectId())
                        .withString("subjectName", mSubjectList.get(mCurSubjectPosition).getSubjectName())
                        .withInt("examId",item.getExamId())
                        .withInt("publishId",item.getExamPublishId())
                        .withString("examName",item.getExamName())
                        .navigation();
            }
        });
    }


    public interface OnFragmentInteractionListener {

    }
}
